package com.boot.json.deserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

import java.io.IOException;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description json String转Long
 * @date 2020/1/13 17:36
 **/

public class StringToLongDeserializer extends StdDeserializer<Long> {

    public static final StringToLongDeserializer INSTANCE = new StringToLongDeserializer();

    protected StringToLongDeserializer() {
        super(String.class);
    }

    @Override
    public Long deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String v = StringDeserializer.instance.deserialize(jsonParser, deserializationContext);
        if (v == null || v.isEmpty()) {
            return null;
        }
        return Long.valueOf(v);
    }
}
